<?php
/**
 * The Template for displaying all single posts.
 *
 */
 
// define current page: will determine which javascript to load and what styles
$CURRENT_PAGE = 'add_new';

// if not logged in, redirect to login page
ns_auth_redirect_login();
 
// POST
$http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
 
// Is post inserted?
$post_inserted = false;

// Error generator
if ( empty($wp_error) )
	$wp_error = new WP_Error();

get_header(); 

if ( $http_post ) {
		
	// Check all the required fields
	if ( empty( $_POST['word'] ) )
		$wp_error->add('empty_word', __('<strong>ERROR</strong>: You forgot to enter the word!', ns_text_domaine));
	if ( empty( $_POST['content'] ) )
		$wp_error->add('empty_content', __('<strong>ERROR</strong>: You forgot to enter the definition!', ns_text_domaine));
		
	if ( !$wp_error->get_error_code() )
	{
		$new_entry_id = ns_add_word( $_POST, $wp_error );
		
		if ( !is_wp_error($new_entry_id) )
		{
			$post_inserted = true;
			$the_new_post = get_post($new_entry_id);
		}
	}
}

$add_relation = false;
if( $_GET['action'] == 'add_relation')
	$add_relation = true;
?>

<div id="wrapper_main" class="grid_24">

	<div id="page_title" class="grid_24">
		<h1><?php 		
				if( $add_relation )
					printf( __('Add a new relationship to "%s"', ns_text_domaine), get_the_title( $_GET['word_id'] ));
				else
					_e( 'Add a new word', ns_text_domaine );		
		?></h1>
	</div>
	
	<div id="add_new" class="grid_24">
		
		<div id="errors_box">	
		<?php		
		if ( $wp_error->get_error_code() ) {
			echo '<div class="clear"></div>';
			$errors = '';
			$messages = '';
			foreach ( $wp_error->get_error_codes() as $code ) {
				$severity = $wp_error->get_error_data($code);
				foreach ( $wp_error->get_error_messages($code) as $error ) {
					if ( 'message' == $severity )
						$messages .= '	' . $error . "<br />\n";
					else
						$errors .= '	' . $error . "<br />\n";
				}
			}
			if ( !empty($errors) )
				echo '<div id="add_word_error" class="error">' . apply_filters('add_word_errors', $errors) . "</div>\n";
			if ( !empty($messages) )
				echo '<div id="add_word_message" class="message">' . apply_filters('add_word_messages', $messages) . "</div>\n";
		}
		?>
		</div>
		
		<?php if($post_inserted) :?>
		
		<p><?php _e( 'Thank you!', ns_text_domaine ); ?></p>
		
		<p><?php _e( 'Your new entry is inserted in the dictionary.', ns_text_domaine ); ?></p>
		
		<p><?php printf( __('View it live here: <a href="%1$s">%2$s</a>.', ns_text_domaine), get_permalink($the_new_post->ID), $the_new_post->post_title); ?></p>
		
		<?php else :?>
		
		<form id="add_new_word" name="add_new_word" method="post" action="#" class="big_clean" accept-charset="utf-8">
			<input type="hidden" name="action" value="<?php if( $add_relation ) echo 'add_relation'; else echo 'add_entry';?>">
			
			<?php if( $add_relation ): ?>
				
			<input type="hidden" name="relative_post_id" value="<?php echo $_GET['word_id'];?>">
			
			<div class="row grid_24">
				<label><?php _e( 'Relation type', ns_text_domaine ); ?></label><br />
				
				<fieldset class="relation_type">				

				<span><input id="relation_type_syn" type="radio" name="relation_type" value="syn" >
				<label for="relation_type_syn"><?php _e( 'Synonym', ns_text_domaine ); ?></label></span>

				<span><input id="relation_type_ant" type="radio" name="relation_type" value="ant" >
				<label for="relation_type_ant"><?php _e( 'Antonym', ns_text_domaine ); ?></label></span>
		
				<span><input id="relation_type_0" type="radio" name="relation_type" value="0" checked="checked" >
				<label for="relation_type_0"><?php _e( 'Not specified', ns_text_domaine ); ?></label></span>	
		
				</fieldset>					
			</div>
			
			<?php endif; ?>
			
			<div class="row grid_24">
				<label for="word"><?php _e( 'The Word', ns_text_domaine ); ?></label>
				
				<input class="validate[required] rounded" name="word" id="word" type="text" value="<?php echo $_POST['word'];?>" size="40" autocomplete="off" tabindex="1">
				<span id="ajax_loader"><?php _e( 'Checking against entry duplication..', ns_text_domaine ); ?></span>
				<p><?php _e( 'For technical reasons only latin words are supported. Ex. "amawal"', ns_text_domaine ); ?></p>
				<p><?php _e( 'Use the keyboard bellow to type special characters.', ns_text_domaine ); ?></p>
				<table id="characters_table">
					<tbody>
					<tr> 
						<td><input type="button" class="special_latin" value="ḍ"></td>
						<td><input type="button" class="special_latin" value="ḥ"></td>
						<td><input type="button" class="special_latin" value="ɛ"></td>
						<td><input type="button" class="special_latin" value="ṛ"></td>
						<td><input type="button" class="special_latin" value="ɣ"></td>
						<td><input type="button" class="special_latin" value="ṣ"></td>
						<td><input type="button" class="special_latin" value="ṭ"></td>
						<td><input type="button" class="special_latin" value="ẓ"></td>
					</tr>
					</tbody>
				</table>
			</div>
			
			<div class="row grid_24">
				<span class="generated_tifinaghe tz">-</span>					
				<p><?php _e( 'How the word will be interpreted in the Tifinagh.', ns_text_domaine ); ?></p>
			</div>
			
			<div class="row grid_24">
				<div class="grid_13 alpha">
					<label><?php _e( 'Used in', ns_text_domaine ); ?></label>
					
					<fieldset class="meta_word_region">		
						<span><input type="checkbox" name="meta_word_region[]" value="cnu" id="meta_word_region_cnu">
						<label for="meta_word_region_cnu"><?php _e( 'Hachenwit', ns_text_domaine ); ?></label></span>
						
						<span><input type="checkbox" name="meta_word_region[]" value="shy" id="meta_word_region_shy">
						<label for="meta_word_region_shy"><?php _e( 'Tacawit', ns_text_domaine ); ?></label></span>
						
						<span><input type="checkbox" name="meta_word_region[]" value="shi" id="meta_word_region_shi">
						<label for="meta_word_region_shi"><?php _e( 'Tachelhit', ns_text_domaine ); ?></label></span>
						
						<span><input type="checkbox" name="meta_word_region[]" value="tmh" id="meta_word_region_tmh">
						<label for="meta_word_region_tmh"><?php _e( 'Tamasheq', ns_text_domaine ); ?></label></span>
						
						<span><input type="checkbox" name="meta_word_region[]" value="tzm" id="meta_word_region_tzm">
						<label for="meta_word_region_tzm"><?php _e( 'Tamazight Atlas', ns_text_domaine ); ?></label></span>
						
						<span><input type="checkbox" name="meta_word_region[]" value="kab" id="meta_word_region_kab">
						<label for="meta_word_region_kab"><?php _e( 'Taqbaylit', ns_text_domaine ); ?></label></span>
						
						<span><input type="checkbox" name="meta_word_region[]" value="rif" id="meta_word_region_rif">
						<label for="meta_word_region_rif"><?php _e( 'Tarifit', ns_text_domaine ); ?></label></span>
						
						<span><input type="checkbox" name="meta_word_region[]" value="mzb" id="meta_word_region_mzb">
						<label for="meta_word_region_mzb"><?php _e( 'Tumzabt', ns_text_domaine ); ?></label></span>
						
						<span><input type="checkbox" name="meta_word_region[]" value="zen" id="meta_word_region_zen">
						<label for="meta_word_region_zen"><?php _e( 'Znaga', ns_text_domaine ); ?></label></span>
					</fieldset>
				</div>
				<div class="grid_10 omega prefix_1">
					<label><?php _e( 'Part of speech', ns_text_domaine ); ?></label>
					
					<fieldset class="relation_type">
						<span><input id="part_of_speech_adj" type="radio" name="part_of_speech" value="adj" >
						<label for="part_of_speech_adj"><?php _e( 'Adjective', ns_text_domaine ); ?></label></span>
		
						<span><input id="part_of_speech_adv" type="radio" name="part_of_speech" value="adv" >
						<label for="part_of_speech_adv"><?php _e( 'Adverb', ns_text_domaine ); ?></label></span>
		
						<span><input id="part_of_speech_n" type="radio" name="part_of_speech" value="n">
						<label for="part_of_speech_n"><?php _e( 'Noun', ns_text_domaine ); ?></label></span>
		
						<span><input id="part_of_speech_v" type="radio" name="part_of_speech" value="v" >
						<label for="part_of_speech_v"><?php _e( 'Verb', ns_text_domaine ); ?></label></span>
		
						<span><input id="part_of_speech_0" type="radio" name="part_of_speech" value="0" checked="checked"  >
						<label for="part_of_speech_0"><?php _e( 'Not specified', ns_text_domaine ); ?></label></span>			
		
						<!--<span><input id="part_of_speech_prep" type="radio" name="part_of_speech" value="prep" >
						<label for="part_of_speech_prep">Preposition</label></span>
		
						<span><input id="part_of_speech_pron" type="radio" name="part_of_speech" value="pron" >
						<label for="part_of_speech_pron">Pronoun</label></span>-->
		
						<!--<span><input id="part_of_speech_conj" type="radio" name="part_of_speech" value="conj" >
						<label for="part_of_speech_conj">Conjunction</label></span>-->		
					</fieldset>
				</div>
			</div>
			
			<div class="clear"></div>
			
			<div class="row grid_24">
				<label for="content"><?php _e( 'Definition in Tamazi&#611;t', ns_text_domaine ); ?></label>
				<textarea class="validate[required] rounded" name="content" id="content" rows="5" cols="40" tabindex="2"><?php echo $_POST['content'];?></textarea>
				<p><?php _e( 'Define this word in Tamazi&#611;t and/or give an example of its use in a sentence.', ns_text_domaine ); ?></p>
			</div>
			
			<div class="row grid_24">
			
				<div class="grid_12 alpha">
					<label for="translation"><?php _e( 'Translation', ns_text_domaine ); ?></label>
					<textarea class="rounded" name="translation[1]" id="translation" rows="5" cols="40" tabindex="2"><?php echo $_POST['translation'];?></textarea>
					<p><?php _e( 'Define this word in selected translation.', ns_text_domaine ); ?></p>
				</div>
				
				<div class="grid_11 prefix_1 omega">
					<label for="content"><?php _e( 'Translation language', ns_text_domaine ); ?></label><br />
					
					<fieldset class="translation_language">
						<span><input id="translation_language_ar" type="radio" name="translation_language[1]" value="ar" >
						<label for="translation_language_ar"><?php _e( 'Arabic', ns_text_domaine ); ?></label></span>
		
						<span><input id="translation_language_en" type="radio" name="translation_language[1]" value="en" >
						<label for="translation_language_en"><?php _e( 'English', ns_text_domaine ); ?></label></span>
		
						<span><input id="translation_language_es" type="radio" name="translation_language[1]" value="es">
						<label for="translation_language_es"><?php _e( 'Spanish', ns_text_domaine ); ?></label></span>
		
						<span><input id="translation_language_fr" type="radio" name="translation_language[1]" value="fr" >
						<label for="translation_language_fr"><?php _e( 'French', ns_text_domaine ); ?></label></span>
		
						<span><input id="translation_language_0" type="radio" name="translation_language[1]" value="0" checked="checked"  >
						<label for="translation_language_0"><?php _e( 'No translation', ns_text_domaine ); ?></label></span>
					</fieldset>
					<p><?php _e( 'Please specify a translation language.', ns_text_domaine ); ?></p>
				</div>
			</div>
			
			<div class="row">
				<input type="submit" name="submit" id="add_new_word_submit" class="rounded" value="<?php _e( 'Add the new word', ns_text_domaine ); ?>">
			</div>
			
		</form>
		<script>
			jQuery(document).ready(function($){
				
				$(".special_latin").click(function(){
					$('input[name="word"]').insertAtCaret( $(this).val() );
					update_tifinagh(  );
				});
				
				$("#add_new_word").validationEngine('attach');
				
				$("input[name=word]").keypress(function() {
					setTimeout(function() {	$("span.generated_tifinaghe").update_tifinagh( 'input[name="word"]' ) }, 50);
				});
				
				$("input[name=word]").focus(function() {
					setTimeout(function() {	$("span.generated_tifinaghe").update_tifinagh( 'input[name="word"]' ) }, 50);
				});
				
				$("input[name=word]").keydown(function() {
					setTimeout(function() {	$("span.generated_tifinaghe").update_tifinagh( 'input[name="word"]' ) }, 50);
				});
				
				$("input[name=word]").focusout(function() {
					
					/*$("#add_new_word_submit").attr('disabled', 'disabled');*/
					/*$("#add_new_word_submit").removeAttr('disabled');*/
					var new_word = $('input[name="word"]').val();
					
					// return if empty
					if(new_word == '') return false;
					
					$("#errors_box").html('');
					$("#errors_box").hide();
					/*$("#ajax_loader").show();*/
					
					
					var the_data = {
						action	: 'check-entry-duplication',
						word	: new_word
					};
					
					// http://api.jquery.com/jQuery.ajax/
					$.ajax({
					    url			: "<?php echo admin_url('admin-ajax.php'); ?>",
					    type		: "POST",
					    data		: the_data,
					    dataType	: 'json',
					    success		: function(response) {
							/*$("#ajax_loader").hide();*/

							if(response.status == 'is_duplicate') {
								$("#errors_box").append('<div class="clear"></div><div id="add_word_error" class="error">'+response.msg+'</div>\n');
								$("#errors_box").show();
								console.log(response.status);
							} else {
								$("#errors_box").hide();
							}
					    },
						error: function(xhr, textStatus, errorThrown){
							console.log(xhr+'|'+textStatus+'|'+errorThrown);
						}
					});
						
					return false;
				});
			});
		</script>
		
		<?php endif;?>
	
	</div>
	
</div>
<!--End #wrapper_main-->

<?php get_footer(); ?>
